<template>
  <div style="position:relative;min-height:100vh;">
    <div class="login-logo">
      <img class="login-logo__img" src="@/assets/images/hsy-logo.png" alt="logo" />
    </div>
    <div class="login-form">
      <van-cell-group>
        <van-field
          v-model="model.userName.value"
          required
          clearable
          :label="model.userName.label"
          placeholder="请输入手机号"
        />
        <van-field
          v-model="model.sms.value"
          required
          center
          clearable
          :label="model.sms.label"
          placeholder="请输入短信验证码"
        >
          <van-button
            slot="button"
            size="small"
            type="danger"
            :disabled="codeDisabled"
            @click="sendCode"
          >{{codeTxt}}</van-button>
        </van-field>
      </van-cell-group>
      <div class="mtb-30 plr-20">
        <van-button type="danger" size="large" @click="submit">登录</van-button>
      </div>
      <p class="tac">或</p>
      <div class="mtb-30 plr-20">
        <van-button type="default" size="large" to="/login" replace>账号密码登录</van-button>
      </div>
    </div>

    <div class="has-bottom"></div>
    <!-- 协议 -->
    <div class="mod-protocol flex flex-h-c">
      <van-checkbox v-model="checked" checked-color="#f44" icon-size="0.4rem" label-disabled>
        <span>登录注册表示您已阅读并同意</span>
        <router-link to="/userProtocol" class="color-danger">《用户协议》</router-link>
        <span>和</span>
        <router-link to="/userPrivacy" class="color-danger">《用户隐私》</router-link>
      </van-checkbox>
    </div>
  </div>
</template>

<script>
import { Toast } from 'vant'
import { sendSms } from 'api/sms'
import { smsLogin } from 'api/member'
export default {
  data() {
    return {
      model: {
        userName: {
          label: '手机号',
          value: '',
          require: true,
          requireMessage: '请输入手机号',
          pattern: /^1[3456789]\d{9}$/,
          patternMessage: '请输入正确的手机号'
        },
        sms: {
          label: '短信验证码',
          value: '',
          require: true,
          requireMessage: '请输入短信验证码'
        }
      },
      codeDisabled: false,
      codeTxt: '发送验证码',
      checked: true
    }
  },
  methods: {
    sendCode() {
      let self = this
      if (!/^1[3456789]\d{9}$/.test(self.model.userName.value)) {
        Toast('请输入正确的手机号')
      } else {
        sendSms(self.model.userName.value, 1).then(res => {
          if (res) {
            Toast.success('短信验证码已发送')
            self.codeDisabled = true
            let time = 60
            self.codeTxt = time + 's'
            let clock = setInterval(() => {
              if (time == 1) {
                self.codeDisabled = false
                self.codeTxt = '发送验证码'
                clearInterval(clock)
              } else {
                self.codeTxt = --time + 's'
              }
            }, 1000)
          }
        })
      }
    },
    submit() {
      let self = this
      // 表单验证
      if (self.$utils.formValidate(self.model).valid) {
        // 验证通过
        if (self.checked) {
          smsLogin(self.model).then(res => {
            if (res) {
              Toast({
                type: 'success',
                message: '登录成功',
                duration: 1000,
                forbidClick: true,
                onClose: function() {
                  //登录成功，设置token
                  self.$store.dispatch('user/setToken', res.Data)
                  self.$store.dispatch('user/login', true)
                  localStorage.setItem('Token', res.Data.Token)
                  localStorage.setItem('RefreshToken', res.Data.RefreshToken)
                  localStorage.setItem('isLogin', '1')
                  self.$router.replace({
                    path: self.$store.getters.redirect ? self.$store.getters.redirect : '/'
                  })
                }
              })
            }
          })
        } else {
          Toast('请同意用户协议与用户隐私！')
        }
      } else {
        Toast(self.$utils.formValidate(self.model).msg)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
